package
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;

	public class Utils
	{
		public function Utils()
		{
			//constructor
		}
		
		public static function roundPositions(displayObjectContainer:DisplayObjectContainer):void
		{
			if (!(displayObjectContainer is Stage)) {
				displayObjectContainer.x = Math.round(displayObjectContainer.x);
				displayObjectContainer.y = Math.round(displayObjectContainer.y);
			}
			for (var i:uint = 0; i < displayObjectContainer.numChildren; i++) {
				var child:DisplayObject = displayObjectContainer.getChildAt(i);
				if (child is DisplayObjectContainer) {
					roundPositions(child as DisplayObjectContainer);
				} else {
					child.x = Math.round(child.x);
					child.y = Math.round(child.y);
				}
			}
		}
		
		public static fnction randomizeArray(a:Array):Array {
			for (var i:uint = 0; i < a.length; i++)
			{
				var rand:uint = int(Math.random() * a.length);
				a.push( a.splice( rand, 1 )[0] );
			}
			return a;
		}
		
		public static function removeDuplicate(arr:Array) : Array{
			var i:int;
			var j: int;
			for (i = 0; i < arr.length - 1; i++){
				for (j = i + 1; j < arr.length; j++){
					if (arr[i] === arr[j]){
						arr.splice(j, 1);
					}
				}
			}
			return arr;
		}
		
		var arr:Array = new Array("a", "b", "a", "c", "d", "b");
		removeDuplicate(arr);
		
		public static function stripSpaces(string:String):String {
				var s:String = string;
				return s.split(" ").join("");
		}
		
		public static function stripTags(string:String):String {
			var s:String = string;
			var regexp:RegExp = new RegExp("<[^<]*>", "gi");
			return s.replace(regexp, "");
		}
		
		public static function slugify(string:String):String {
			const pattern1:RegExp = /[^\w- ]/g; // Matches anything except word characters, space and -
			const pattern2:RegExp = / +/g; // Matches one or more space characters
			var s:String = string;
			return s.replace(pattern1, "").replace(pattern2, "_").toLowerCase();
		}
			
		
		//TODO test this function
		public static function createGrid(displayObject:DisplayObject,c:Class):void {
			for (var i:uint = 0; i < 20; i++)
			{
				var displayObject:MyDisplayObject = new MyDisplayObject();
				displayObject.x = displayObject.width * ( i % 5 );
				displayObject.y = displayObject.height * Math.floor( i / 5 );
				addChild(displayObject);
			}
		}
		
		public static function removeAllChildren(displayObjectContainer:DisplayObjectContainer):void {
			while (displayObjectContainer.numChildren) {
				displayObjectContainer.removeChildAt(0);   
			}
		}
	}
}